package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.VariationValue;
import com.ericom.service.GenericManager;

public class VariationValueAction extends BaseAction {
	private GenericManager<VariationValue, Long> variationValueManager;
	private List variationValues;
	private VariationValue variationValue;
	private Long id;

	public VariationValue getVariationValue() {
		return variationValue;
	}

	public void setVariationValue(VariationValue variationValue) {
		this.variationValue = variationValue;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setVariationValueManager(
			GenericManager<VariationValue, Long> variationValueManager) {
		this.variationValueManager = variationValueManager;
	}

	public String list() {
		variationValues = variationValueManager.getAll();
		return SUCCESS;
	}

	public List getVariationValues() {
		return variationValues;
	}

	public String delete() {
		variationValueManager.remove(variationValue.getId());
		this.saveMessage(getText("variationValue.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			variationValue = variationValueManager.get(id);
		} else {
			variationValue = new VariationValue();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (variationValue.getId() == null);
		variationValue = variationValueManager.save(variationValue);

		String key = (isNew) ? "variationValue.added"
				: "variationValue.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
